import uart from '../dxmodules/dxUart.js'
import std from '../dxmodules/dxStd.js'
import logger from '../dxmodules/dxLogger.js'

// id
const id = "uart"

// 485 serial port address
const UART_485 = "/dev/ttyS3"

// Open 485 serial port
uart.open(uart.TYPE.UART, UART_485, id)

// Set the serial port baud rate
uart.ioctl(1, '115200-8-N-1', id)

// Receive data
std.setInterval(() => {
    let byteArr = uart.receive(1, 100, id)
    if (byteArr && byteArr.length > 0) {
        logger.info(JSON.stringify(byteArr));
        // Send serial port data, return as is
        let buffer = new ArrayBuffer(byteArr.length);
        let uint8View = new Uint8Array(buffer);
        for (let i = 0; i < byteArr.length; i++) {
            uint8View[i] = byteArr[i];
        }
        uart.send(buffer, id)
    }
}, 10)